﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class my_setdefaultdata : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ctpDataContext db = new ctpDataContext();

        //reset users' nickname

        var user_data = from p in db.users
                        select p;

        string nickname;

        foreach (user u in user_data)
        {
            if (u.nickname.Trim().Length > 10)
            {
                nickname=u.nickname.Trim();

                if (nickname.IndexOf('@') != -1)
                {
                    nickname = nickname.Substring(0, nickname.IndexOf('@'));
                }

                if (nickname.Length > 10)
                {
                    nickname = nickname.Substring(0, 10);
                }

                u.nickname = nickname;
            }

        }

        db.SubmitChanges();
        


        //set users' default cities

        var data=from p in db.ct_s_intros
                 select p;

        foreach (ct_s_intro i in data)
        {
            this.addthis((int)i.create_user_no, (int)i.ct_no);
        }


        //設定預設的圖片

        var data1 = from p in db.users
                    where p.profile_picture == null
                    select p;

        foreach (user u in data1)
        {
            u.profile_picture = "coming.gif";

        }

        var data2 = from p in db.ct_s_profiles
                    where p.main_pic_fn == null
                    select p;

        foreach (ct_s_profile c in data2)
        {
            c.main_pic_fn = null;
        }

        db.SubmitChanges();


        foreach (ct_s_profile c in data2)
        {
            c.main_pic_fn = "coming.gif";
        }





        var data3 = from p in db.ct_profiles
                    where p.main_pic_fn == null
                    select p;

        foreach (ct_profile c in data3)
        {
            
            c.main_pic_fn = "coming.gif";
        }



        db.SubmitChanges();



    }

    void addthis(int userno, int ctno)
    {

        ctpDataContext db = new ctpDataContext();

        if (ctno == -1) return;

        int check = db.my_citiport_mines.Count(p => p.userno == userno && p.ct_no == ctno);
        if (check != 0) return;

        

        my_citiport_mine mi = new my_citiport_mine();
        mi.addtime = DateTime.Now;
        mi.ct_no =ctno;
        mi.userno = userno;
        mi.last_update_time = DateTime.Now;

        db.my_citiport_mines.InsertOnSubmit(mi);
        db.SubmitChanges();
    }
}
